package com.shqcjd.sinopecdemomobile.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.shqcjd.sinopecdemomobile.R;
import com.shqcjd.sinopecdemomobile.api.ApiService;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxObserver;
import com.shqcjd.sinopecdemomobile.helper.rxjavahelper.RxSchedulersHelper;
import com.shqcjd.sinopecdemomobile.model.ResponseData;
import com.shqcjd.sinopecdemomobile.model.pojo.KnowledgeInfoBean;
import com.shqcjd.sinopecdemomobile.ui.base.BaseActivity;
import com.shqcjd.sinopecdemomobile.ui.base.BasePresenter;
import com.shqcjd.sinopecdemomobile.util.ToastUtils;
import com.shqcjd.sinopecdemomobile.util.UIUtils;
import com.shqcjd.sinopecdemomobile.widget.FlowLayout;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class KnowledgeInfoActivity extends BaseActivity {

    @BindView(R.id.tv_include_title)
    TextView tvIncludeTitle;
    @BindView(R.id.tv_include_finish)
    Button tvIncludeFinish;
    @BindView(R.id.fl_knowledge_info)
    FlowLayout flKnowledgeInfo;
    @BindView(R.id.iv_knowledge_info_pic)
    ImageView ivKnowledgeInfoPic;
    @BindView(R.id.tv_knowledge_info_title)
    TextView tvKnowledgeInfoTitle;
    @BindView(R.id.tv_knowledge_info_name)
    TextView tvKnowledgeInfoName;
    @BindView(R.id.tv_knowledge_info_time)
    TextView tvKnowledgeInfoTime;
    @BindView(R.id.tv_knowledge_info_length)
    TextView tvKnowledgeInfoLength;
    @BindView(R.id.tv_knowledge_info_person)
    TextView tvKnowledgeInfoPerson;
    @BindView(R.id.tv_knowledge_info_agree)
    TextView tvKnowledgeInfoAgree;
    @BindView(R.id.rl_knowledge_info_agree)
    RelativeLayout rlKnowledgeInfoAgree;
    @BindView(R.id.iv_knowledge_info_play)
    ImageView ivKnowledgeInfoPlay;

    private Context context;
    private int kId;
    private int isAgree = -1;//-1代表不能点赞
    private String url, name;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_knowledge_info;
    }

    @Override
    public void init() {
        super.init();
        context = this;
        kId = getIntent().getIntExtra("kId", -1);
//        kId = 4;
    }

    @Override
    public void initView() {
        super.initView();
        tvIncludeTitle.setText("知识库详情");
        tvIncludeFinish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        rlKnowledgeInfoAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(isAgree!=-1){
                    getAgreeHttp();
                }
            }
        });
        ivKnowledgeInfoPic.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!url.equals("")){
                    Intent it = new Intent(context, UIUtils.previewClass(UIUtils.getFormatName(url)));
                    it.putExtra("name", name);
                    it.putExtra("url", url);
                    startActivity(it);
                }else{
                    ToastUtils.showShort(context, "链接存在问题");
                }
            }
        });
        getData();
    }

    private TextView mTextView(String str) {
        TextView textView = new TextView(context);
        textView.setText(str);
        textView.setTextSize(12);
        textView.setBackgroundDrawable(getResources().getDrawable(R.drawable.shape_knowledge_info_round));
        textView.setTextColor(Color.parseColor("#94a0a8"));
        return textView;
    }

    private void getAgreeHttp(){
        ApiService.getKnowledgeInfoAgree(kId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData retData) {
                        if (retData.retCode == 10000 && retData.data != null) {
                            ToastUtils.showShort(context, "点赞成功");
                            tvKnowledgeInfoAgree.setText("已赞");
                            isAgree = -1;
                        } else {
                            ToastUtils.showError(context);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        ToastUtils.showError(context);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

    private void getData() {
        ApiService.getKnowledgeInfo(kId)
                .compose(RxSchedulersHelper.io_main())
                .subscribe(new RxObserver<ResponseData<KnowledgeInfoBean>>() {
                    @Override
                    public void _onSubscribe(Disposable d) {
                        super._onSubscribe(d);
                        showWaitingDialog("正在加载...");
                    }

                    @Override
                    public void _onNext(ResponseData<KnowledgeInfoBean> retData) {
                        if (retData.retCode == 10000 && retData.data != null) {
                            ViewGroup.MarginLayoutParams lp = new ViewGroup.MarginLayoutParams(
                                    ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            lp.rightMargin = UIUtils.dp2px(7);
                            lp.topMargin = UIUtils.dp2px(5);
                            for (int i = 0; i < retData.data.tags.size(); i++) {
                                flKnowledgeInfo.addView(mTextView(retData.data.tags.get(i)), lp);
                            }

                            tvKnowledgeInfoTitle.setText(retData.data.projectName);
                            tvKnowledgeInfoName.setText("上传者:" + retData.data.userName);
                            tvKnowledgeInfoTime.setText("上传时间:" + retData.data.uploadTime);
                            tvKnowledgeInfoLength.setText("视频时长:" + retData.data.timeLength);
                            if(!retData.data.likePeople.equals("")){
                                tvKnowledgeInfoPerson.setVisibility(View.VISIBLE);
                                tvKnowledgeInfoPerson.setText("  " + retData.data.likePeople + "等赞了这条");
                            }else{
                                tvKnowledgeInfoPerson.setVisibility(View.INVISIBLE);
                            }
                            if(retData.data.like){
                                tvKnowledgeInfoAgree.setText("已赞");
                            }else{
                                isAgree = 0;
                                tvKnowledgeInfoAgree.setText("赞一下");
                            }
                            url = retData.data.videoUrl;
                            name = retData.data.projectName;
                            if(!UIUtils.getFormatName(url).equals(".mp4")){
                                ivKnowledgeInfoPlay.setVisibility(View.GONE);
                            }

                            RequestOptions options = new RequestOptions()
                                    .placeholder(R.mipmap.ic_default_small)
                                    .error(R.mipmap.ic_default_small)
                                    .centerCrop();
                            if (url != null) {
                                String suffix = url.substring(url.lastIndexOf(".")).toLowerCase();
                                switch (suffix) {
                                    case ".doc":
                                    case ".docx":
                                        Glide.with(context)
                                                .load(R.mipmap.ic_doc)
                                                .apply(options)
                                                .into(ivKnowledgeInfoPic);
                                        break;
                                    case ".pdf":
                                        Glide.with(context)
                                                .load(R.mipmap.ic_pdf)
                                                .apply(options)
                                                .into(ivKnowledgeInfoPic);
                                        break;
                                    case ".jpg":
                                    case ".png":
                                    case ".jpeg":
                                        Glide.with(context)
                                                .load(url)
                                                .apply(options)
                                                .into(ivKnowledgeInfoPic);
                                        break;
                                    default:
                                        Glide.with(context)
                                                .load(retData.data.videoUrl+"?x-oss-process=video/snapshot,t_10000,f_jpg,w_800,h_600")
                                                .apply(options)
                                                .into(ivKnowledgeInfoPic);
                                        break;
                                }
                            }


                        } else {
                            ToastUtils.showError(context);
                        }
                    }

                    @Override
                    public void _onError(String errorMessage) {
                        Log.e("tou", errorMessage);
                        ToastUtils.showError(context);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        hideWaitingDialog();
                    }
                });
    }

}
